<?php
require_once("Model/Model.php");

class mCapsuleChecker extends Model {

    function __construct() {
        parent::__construct();
    }

	function reg($nick,$email,$pass,$lang,$udid,$date_sql,$acc_token){
		$result = $this->select()->from('pla2_account')->where("acc_email = '{$email}'")->query_select()->num_rows;
		if ( $result > 0 ) {
			$data['success'] = 1;
			return $data;
		} else {
			$id = $this->get_maxfield("pla2_account","acc_id")+1;
			$this->insert( pla2_acc_token , array( acc_token => $acc_token , acc_token_email => $email , acc_token_password => $pass ,  acc_token_udid => $UDID ,acc_token_exp => $date_sql , acc_token_lang => $lang ));
			$this->insert( pla2_account , array( acc_id => $id , acc_user => $nick , acc_email => $email , acc_pass => $pass , acc_type => "normal", acc_lang => $lang ));
			$data['success'] = 0;
			$data['acc_id'] = $id;
			return $data;
		}
	}

	 function login($email,$pass,$lang,$UDID,$acc_token,$date_sql) {
	 	// errCode
	 	// 0 = ไม่มี email
	 	// 1 = พาสเวิดผิด
		// 2 = login success
	 	$check_email = $this->select('acc_email')->from('pla2_account')->where("acc_email = '{$email}' ")->query_select();
		if ( $check_email->num_rows == 0 ) {
			// 0 = ไม่มี email
			$data['success'] = 0;
			return $data;
		}
		
	 	$result = $this->select()->from('pla2_account')->where("acc_email = '{$email}' and acc_pass = '{$pass}' ")->query_select();
		
		if ( $result->num_rows > 0 ) {
			$a = $result->fetch_array();
			// login success
			$data['success'] = 2;
			$data['acc_id'] = $a['acc_id'];
			$data['acc_user'] = $a['acc_user'];
			$data['acc_token']= $acc_token;
			$this->insert( pla2_acc_token , array( acc_token => $acc_token , acc_token_email => $email , acc_token_password => $pass ,  acc_token_udid => $UDID ,acc_token_exp => $date_sql , acc_token_lang => $lang ));
			return $data;
		} else {
			// 1 = พาสเวิดผิด
			$data['success'] = 1;
			return $data;
		}
	 }
	 
	 function tklogin($token,$lang,$UDID,$acc_token,$date_sql) {
	 	$check_token = $this->select()->from('pla2_acc_token')->where("acc_token = '{$token}'")->query_select();
		if ( $check_token->num_rows == 0 ) {
			// errCode 1
			// ไม่มี token นี้
			$data['success'] = 1;
			return $data;
		} else {
			// errCode 0
			// อัพเดท เสร้จสิ้น
			$a = $check_token->fetch_array();
			$email = $a['acc_token_email'];
			$pass = $a['acc_token_password'];
			$b = $this->select()->from('pla2_account')->where("acc_email = '{$email}' and acc_pass = '{$pass}' ")->query_select();
			$c = $b->fetch_array();
			$this->update('pla2_acc_token',array(acc_token => $acc_token , acc_token_exp => $date_sql ),"acc_token = '{$token}' ");
			$data['acc_id'] = $c['acc_id'];
			$data['acc_user'] = $c['acc_user'];
			$data['acc_token'] = $acc_token;
			$data['success'] = 0;
			return $data;
		}
	 }
	 
	 function logout($token) {
	 	$check_token = $this->select('acc_token')->from('pla2_acc_token')->where("acc_token = '{$token}'")->query_select();
		if ( $check_token->num_rows == 0 ) {
			// errCode 1
			// ไม่มี token นี้
			$data['success'] = 1;
			return $data;
		} else {
			// errCode 0
			// ลบ เสร้จสิ้น
			$this->delete( 'pla2_acc_token' , "acc_token = '{$token}'");
			$data['success'] = 0;
			return $data;
		}
	 }
	 
	 function fblogin($facebook_email,$facebook_nick,$date_sql,$acc_token) {
	 	$this->insert( pla2_acc_token , array( acc_token => $acc_token , acc_token_email => $facebook_email , acc_token_password => $pass ,  acc_token_udid => $UDID ,acc_token_exp => $date_sql , acc_token_lang => $lang ));
		$this->insert( pla2_account , array( acc_id => $id , acc_user => $nick , acc_email => $email , acc_pass => "" , acc_type => "normal", acc_lang => $lang ));
	 }
	 
}
